package org.unidbfw.model;

import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.unidbfw.common.FWConst;
import org.unidbfw.common.UniDBFWException;
import org.unidbfw.utils.QueryUtils;

public class ModelManager implements IModelManager
{

	private static ModelManager inst = null;
	private Document model = null;
	
	private ModelManager() throws UniDBFWException
	{ 
		System.out.println("ModelManager.ModelManager()");
		try
		{
			SAXBuilder builder = new SAXBuilder();
			model = builder.build(FWConst.MODEL_FILENAME);
		}
		catch (JDOMException e)
		{
			throw new UniDBFWException(e.getMessage(), e);
		}
		catch (IOException e)
		{
			throw new UniDBFWException(e.getMessage(), e);
		}		
	}
	
	public static ModelManager getInst() throws UniDBFWException
	{
		System.out.println("ModelManager.getInst()");
		if (inst == null)
		{
			inst = new ModelManager();
		}
		return inst;
	}
	
	public String getDbUrl()
	{
		System.out.println("ModelManager.getDbUrl()");
		Element eConnection = model.getRootElement().getChild(FWConst.MODEL_CONNECTION);
		return eConnection.getChildText(FWConst.MODEL_DB_URL);
	}

	public String getDbLogin()
	{
		System.out.println("ModelManager.getDbLogin()");
		Element eConnection = model.getRootElement().getChild(FWConst.MODEL_CONNECTION);
		return eConnection.getChildText(FWConst.MODEL_DB_LOGIN);
	}

	public String getDbPassword()
	{
		System.out.println("ModelManager.getDbPassword()");
		Element eConnection = model.getRootElement().getChild(FWConst.MODEL_CONNECTION);
		return eConnection.getChildText(FWConst.MODEL_DB_PASSWORD);
	}

	public Element getTable(String tableName)
	{
		System.out.println("ModelManager.getTable()");
		for (Element table : model.getRootElement().getChildren(FWConst.MODEL_DB_TABLE))
		{
			if (table.getAttributeValue(FWConst.MODEL_DB_TABLE_NAME).equals(tableName))
			{
				return table;
			}
		}
		return null;

	}

	public String getTableSelectQuery(String tableName) throws UniDBFWException
	{
		return getTableSelectQuery(getTable(tableName));
	}

	public String getTableSelectQuery(Element table) throws UniDBFWException
	{
		String query = table.getChildText(FWConst.MODEL_DB_TABLE_SELECT);
		if (query == null)
		{
			query = QueryUtils.generateSelectQuery(table.getAttributeValue(FWConst.MODEL_DB_TABLE_NAME));
		}
		return query;
	}

	public String getTableUpdateQuery(String tableName) throws UniDBFWException
	{
		return getTableUpdateQuery(getTable(tableName));
	}

	public String getTableUpdateQuery(Element table) throws UniDBFWException
	{
		String query = table.getChildText(FWConst.MODEL_DB_TABLE_UPDATE);
		if (query == null)
		{
			query = QueryUtils.generateUpdateQuery(table.getAttributeValue(FWConst.MODEL_DB_TABLE_NAME));
		}
		return query;
	}

	public String getTableDeleteQuery(String tableName) throws UniDBFWException
	{
		return getTableDeleteQuery(getTable(tableName));
	}

	public String getTableDeleteQuery(Element table) throws UniDBFWException
	{
		String query = table.getChildText(FWConst.MODEL_DB_TABLE_DELETE);
		if (query == null)
		{
			query = QueryUtils.generateDeleteQuery(table.getAttributeValue(FWConst.MODEL_DB_TABLE_NAME));
		}
		return query;
	}

	public String getTableInsertQuery(String tableName) throws UniDBFWException
	{
		return getTableInsertQuery(getTable(tableName));
	}

	public String getTableInsertQuery(Element table) throws UniDBFWException
	{
		String query = table.getChildText(FWConst.MODEL_DB_TABLE_INSERT);
		if (query == null)
		{
			query = QueryUtils.generateInsertQuery(table.getAttributeValue(FWConst.MODEL_DB_TABLE_NAME));
		}
		return query;
	}
	
}
